package com.example.zhihu.controller;

import com.example.zhihu.pojo.Answer;
import com.example.zhihu.pojo.Comments;
import com.example.zhihu.pojo.Users;
import com.example.zhihu.pojo.vo.*;
import com.example.zhihu.service.AnswerService;
import com.example.zhihu.service.UserService;
import com.example.zhihu.utils.JSONResult;
import com.example.zhihu.utils.PagedResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value="回答接口", tags = {"回答controller"})
@RestController
@RequestMapping("/api/answer")
public class AnswerController {

    @Autowired
    private AnswerService answerService;

    @Autowired
    private UserService userService;

    @ApiOperation("添加回答")
    @PostMapping("/addAnswer")
    public JSONResult addAnswer(@RequestBody Answer answer) {
        Answer res = new Answer();
        res = answerService.addAnswer(answer);
        return JSONResult.ok(res);
    }

    @ApiOperation("删除回答")
    @GetMapping("/delAnswer")
    public JSONResult delAnswer(@RequestParam String id) {
        if(StringUtils.isBlank(id)) {
            return JSONResult.errorMsg("信息有误");
        }
        answerService.delAnswer(id);
        return JSONResult.ok("删除成功");
    }

    @ApiOperation("回答列表")
    @GetMapping("/queryAnswerList")
    public JSONResult answerList(@RequestParam(required = false) String userId,
                                 @RequestParam(required = false)String questionId,
                                 @RequestParam(required = false)String loginUserId,
                                 @RequestParam(required = false)String desc,
                                 @RequestParam(required = false) Integer page) {
        QuestionVO qvo = new QuestionVO();
        if(StringUtils.isNotBlank(questionId)) {
            qvo = answerService.queryOneQuestion(questionId);
        }
        if (page == null) {
            page =1;
        }
        PageHelper.startPage(page, 10);
        List<AnswerVO> alist = answerService.answerList(userId, questionId, desc);
        for(AnswerVO a : alist) {
            String answerId = a.getId();
            boolean userLikeAnswer = userService.isUserLikeAnswer(loginUserId,answerId);
            boolean userCollectAnswer = userService.isUserCollectAnswer(loginUserId,answerId);
            a.setCommentCount(answerService.showCommentCount(answerId));
            a.setCommentList(answerService.getComments(answerId));
            a.setLike(userLikeAnswer);
            a.setCollect(userCollectAnswer);
        }
        PublisherAnswer pa = new PublisherAnswer();
        pa.setQvo(qvo);
        PageInfo<AnswerVO> pageList = new PageInfo<>(alist);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setRecords(pageList.getTotal());
        pagedResult.setPage(page);
        pagedResult.setRows(alist);
        pagedResult.setTotal(pageList.getPages());
        pa.setPagedResult(pagedResult);
        return JSONResult.ok(pa);
    }

    @ApiOperation("热门回答")
    @GetMapping("/hotAnswerList")
    public JSONResult hotAnswer(@RequestParam Integer page) {
        PageHelper.startPage(page, 8);
        List<AnswerVO> list = answerService.hotAnswerList();
        for(AnswerVO a : list) {
            a.setCommentCount(answerService.showCommentCount(a.getId()));
        }

        PageInfo<AnswerVO> pageList= new PageInfo<>(list);
        PagedResult res = new PagedResult();
        res.setTotal(pageList.getPages());
        res.setRows(list);
        res.setPage(page);
        res.setRecords(pageList.getTotal());
        return JSONResult.ok(res);
    }

//    @ApiOperation("查询单个回答")
//    @GetMapping("/queryAnswer")
//    public JSONResult queryAnswer(@RequestParam(required = false) String loginUserId, @RequestParam(required = false) String creatorId, @RequestParam(required = false) String answerId) {
//        Users userInfo = userService.queryUser(creatorId);
//        AnswerVO answerVO = answerService.queryAnswer(answerId);
//        UserVO creator = new UserVO();
//        BeanUtils.copyProperties(userInfo,creator);
//
//        boolean userLikeAnswer = userService.isUserLikeAnswer(loginUserId,answerId);
//        boolean userCollectAnswer = userService.isUserCollectAnswer(loginUserId,answerId);
//        PublisherAnswer publisherAnswer = new PublisherAnswer();
//        publisherAnswer.setPublisher(answerVO);
//        publisherAnswer.setUserCollectAnswer(userCollectAnswer);
//        publisherAnswer.setUserLikeAnswer(userLikeAnswer);
//        return JSONResult.ok(publisherAnswer);
//    }

    @ApiOperation("编辑回答")
    @PostMapping("updateAnswer")
    public JSONResult updateAnswer(@RequestBody Answer answer) {
        if(StringUtils.isBlank(answer.getId())) {
            return JSONResult.errorMsg("信息有误");
        }
        answerService.updateAnswer(answer);
        return JSONResult.ok("修改成功");
    }

    @ApiOperation("显示回答数量")
    @GetMapping("/showNum")
    public JSONResult showNum(@RequestParam String qid) {
        Integer num = answerService.showNum(qid);
        return JSONResult.ok(num);
    }

    @ApiOperation("点赞/收藏回答")
    @GetMapping("/userLike")
    public JSONResult userLike(@RequestParam String userId,@RequestParam String answerId, @RequestParam String creatorId, @RequestParam Integer status) {
        answerService.userLikeAnswer(userId,answerId,creatorId,status);
        return JSONResult.ok("操作成功");
    }

    @ApiOperation("取消点赞/收藏")
    @GetMapping("userUnlike")
    public JSONResult userUnlike(@RequestParam String userId,@RequestParam String answerId, @RequestParam String creatorId) {
        answerService.userUnlikeAnswer(userId,answerId,creatorId);
        return JSONResult.ok("操作成功");
    }

    @ApiOperation("添加评论")
    @PostMapping("/addComment")
    public JSONResult addComment(@RequestBody Comments comment, @RequestParam(required = false) String fatherCommentId, @RequestParam(required = false) String toUserId) {
        if(StringUtils.isNotBlank(fatherCommentId) && StringUtils.isNotBlank(toUserId)) {
            comment.setFatherCommentId(fatherCommentId);
            comment.setToUserId(toUserId);
        }
        answerService.addComment(comment);
        return JSONResult.ok("评论成功");
    }

    @ApiOperation("显示评论")
    @GetMapping("/getComments")
    public JSONResult getComments(@RequestParam String answerId, @RequestParam(required = false) Integer page) {
        if(page == null) {
            page = 1;
        }
        List<CommentVO> list = answerService.getComments(answerId);
        return JSONResult.ok(list);
    }
}
